/**
 * Copyright 2015-现在 广州市领课网络科技有限公司
 */
package com.roncoo.education.course.service.api.biz;

import com.roncoo.education.constants.UploadUtil;
import com.roncoo.education.constants.CourseConstants;
import com.roncoo.education.course.service.dao.CourseChapterPeriodAuditDao;
import com.roncoo.education.course.service.dao.CourseChapterPeriodDao;
import com.roncoo.education.course.service.dao.CourseVideoDao;
import com.roncoo.education.course.service.dao.FileStorageDao;
import com.roncoo.education.course.service.dao.impl.mapper.entity.CourseChapterPeriod;
import com.roncoo.education.course.service.dao.impl.mapper.entity.CourseChapterPeriodAudit;
import com.roncoo.education.course.service.dao.impl.mapper.entity.CourseVideo;
import com.roncoo.education.course.service.dao.impl.mapper.entity.FileStorage;
import com.roncoo.education.system.feign.interfaces.IFeignSys;
import com.roncoo.education.system.feign.vo.SysVO;
import com.roncoo.education.util.aliyun.Aliyun;
import com.roncoo.education.util.aliyun.AliyunUtil;
import com.roncoo.education.util.base.BaseBiz;
import com.roncoo.education.util.base.Result;
import com.roncoo.education.util.config.SystemUtil;
import com.roncoo.education.util.enums.FileStorageTypeEnum;
import com.roncoo.education.util.enums.FileTypeEnum;
import com.roncoo.education.util.enums.PlatformEnum;
import com.roncoo.education.util.enums.VideoStatusEnum;
import com.roncoo.education.util.tools.BeanUtil;
import com.roncoo.education.util.tools.IdWorker;
import com.roncoo.education.util.tools.StrUtil;
import com.roncoo.spring.boot.autoconfigure.fastdfs.FastdfsClientService;
import com.xiaoleilu.hutool.util.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 上传接口
 *
 * @author wuyun
 */
@Component
public class ApiUploadBiz extends BaseBiz {

	@Autowired
	private IFeignSys bossSys;
	@Autowired
	private CourseChapterPeriodAuditDao courseChapterPeriodAuditDao;
	@Autowired
	private CourseChapterPeriodDao courseChapterPeriodDao;

	@Autowired
	private CourseVideoDao courseVideoDao;
	@Autowired
	private FileStorageDao fileStorageDao;
	@Autowired
	private FastdfsClientService fastdfsClientService;

	/**
	 * 上传视频接口
	 *
	 * @author wuyun
	 */
	public Result<String> uploadVideo(MultipartFile multipartFile) {
		//abcdefg.pdf
		String fileFullName = multipartFile.getOriginalFilename();
		//.pdf
		String fileSuffix = fileFullName.substring(fileFullName.lastIndexOf("."), fileFullName.length());
		// abcdefg
		String fileSimpleName = fileFullName.substring(0, fileFullName.lastIndexOf("."));
		//重新生成文件名
		Long HashDigest = IdWorker.getId();
		String fileHashDigestFullName = HashDigest + fileSuffix;
		//先存储到本地 然后异步推送到 云端
		File targetFile = new File(CourseConstants.UPLOAD_WINDOWS_PATH + fileHashDigestFullName);
		// 判断文件目录是否存在，不存在就创建文件目录
		if (!targetFile.getParentFile().exists()) {
			targetFile.getParentFile().mkdirs();
		}
//		try {
//			multipartFile.transferTo(targetFile);
//		} catch (Exception e) {
//			logger.error("上传到本地失败", e);
//			return Result.error("上传文件出错，请重新上传");
//		}
//
////		String resourcePath = "http://10.15.10.58:5710/upload/" + fileHashDigestFullName;
//		String resourcePath = CourseConstants.UPLOAD_RESOURCE_URL + fileHashDigestFullName;

		String resourcePath = UploadUtil.uploadFileByOSS(multipartFile);



		// 新增课程视频表信息
		CourseVideo tempSave = new CourseVideo();
		tempSave.setVideoNo(HashDigest);
		tempSave.setGmtCreate(null);
		tempSave.setGmtModified(null);
		tempSave.setVideoName(fileHashDigestFullName);
		tempSave.setVideoStatus(VideoStatusEnum.WAIT.getCode());
		tempSave.setVideoOasId(resourcePath);
		int result = courseVideoDao.save(tempSave);


		// 根据视频编号、课时ID查询课程视频信息
		CourseVideo courseVideo = courseVideoDao.getByVideoNoAndPeriodId(HashDigest, Long.valueOf(0));

		// 根据视频编号更新视频信息
		List<CourseVideo> list = courseVideoDao.listByVideoNo(HashDigest);
		for (CourseVideo video : list) {
			video.setVideoLength(courseVideo.getVideoLength());
			video.setVideoVid(courseVideo.getVideoVid());
			video.setVideoStatus(VideoStatusEnum.SUCCES.getCode());
			video.setVideoOasId(courseVideo.getVideoOasId());
			courseVideoDao.updateById(video);
		}

		// 更新课时审核表视频信息
		List<CourseChapterPeriodAudit> periodAuditList = courseChapterPeriodAuditDao.listByVideoNo(HashDigest);
		for (CourseChapterPeriodAudit periodAudit : periodAuditList) {
			periodAudit.setVideoName(courseVideo.getVideoName());
			periodAudit.setVideoLength(courseVideo.getVideoLength());
			periodAudit.setVideoVid(courseVideo.getVideoVid());
			courseChapterPeriodAuditDao.updateById(periodAudit);
		}
		// 更新课时视频信息
		List<CourseChapterPeriod> periodList = courseChapterPeriodDao.listByVideoNo(HashDigest);
		for (CourseChapterPeriod period : periodList) {
			period.setVideoName(courseVideo.getVideoName());
			period.setVideoLength(courseVideo.getVideoLength());
			period.setVideoVid(courseVideo.getVideoVid());
			courseChapterPeriodDao.updateById(period);
		}

		return Result.success(String.valueOf(HashDigest));
	}

	/**
	 * 上传图片接口
	 *
	 * @author wuyun
	 */
	public Result<String> uploadPic(MultipartFile multipartFile){
//		String result = uploadPicByLocal(multipartFile);
        String result  = UploadUtil.uploadFileByOSS(multipartFile);

        return Result.success(result);
	}


}
